package com.hk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hk.dao.TeacherDao;
import com.hk.entity.CourseInfo;
import com.hk.entity.PageBean;
import com.hk.entity.Teacher;
import com.hk.service.TeacherService;
import org.apache.ibatis.annotations.Param;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {
    /**
     * 创建日志记录器（Logger）
     */
    private static final Logger logger = LoggerFactory.getLogger(TeacherService.class);

    @Autowired
    private TeacherDao teacherDao;

    /**
     * 增加教师
     * @param teacher
     */
    @Override
    public void teacherAdd(Teacher teacher) {
        teacher.setCreateTime(LocalDateTime.now());
        teacher.setUpdateTime(LocalDateTime.now());
        teacherDao.TeacherAdd(teacher);
    }

    /**
     * 批量删除教师
     * @param ids
     */
    @Override
    public void delete(List<Integer> ids) {
        teacherDao.delete(ids);
    }

    @Override
    public Teacher getById(Integer id) {
        return teacherDao.getById(id);
    }

    @Override
    public void update(Teacher teacher) {
        teacher.setUpdateTime(LocalDateTime.now());
        teacherDao.update(teacher);
    }

    @Override
    public List<Teacher> queryAll() {
        return teacherDao.queryAll();
    }

    @Override
    public List<Teacher> searchTea(Teacher teacher) {
        return teacherDao.searchTea(teacher);
    }
    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    @Override
    public PageBean page(Integer page, Integer pageSize) {
        try {
            // 设置分页参数
            PageHelper.startPage(page, pageSize);
            // 执行查询
            List<Teacher> teacherList = teacherDao.queryAll();
            Page<Teacher> p = (Page<Teacher>) teacherList;
            // 打印生成的 SQL 语句
            logger.info("生成的 SQL 语句: {}", sqlSessionTemplate.getConfiguration().getMappedStatement("com.hk.dao.TeacherDao.queryAll").getBoundSql(null).getSql());
            // 封装 PageBean 对象
            PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
            return pageBean;
        } catch (Exception e) {
            // 记录异常信息
            logger.error("获取教师时出错: ", e);
            // 返回空的 PageBean 对象
            return new PageBean(0L, Collections.emptyList());
        }
    }

    //aaa
    @Override
    public List<CourseInfo> getByIdInfo(String empId) {
        return teacherDao.getByIdInfos(empId);
    }

    @Override
    public void deleteInfo(@Param("list") List<Integer> ids) {
        teacherDao.deleteInfo(ids);
    }

    @Override
    public void addCourse(CourseInfo courseInfo) {
        // 检查教师是否存在
        Teacher teacher = teacherDao.getById(courseInfo.getEmpId());

        if (teacher != null) {
            // 如果教师存在，直接添加课程信息
            teacherDao.addCourse(courseInfo);
        } else {
            throw new IllegalArgumentException("教师信息不存在！");
        }
    }

    @Override
    public void UpdataInfo(CourseInfo courseInfo) {
        teacherDao.UpdataInfo(courseInfo);
    }


}



